import { vi } from 'vitest'
import { config } from '@vue/test-utils'

// 全局配置
config.global.stubs = {
  'svg-icon': true,
  'router-link': true,
  'router-view': true,
  // Element Plus 组件模拟
  'el-pagination': true,
  'el-form': true,
  'el-form-item': true,
  'el-input': true,
  'el-button': true,
  'el-checkbox': true,
  'el-upload': true
}

// 模拟 Vue composition API
vi.mock('vue', async () => {
  const actual = await vi.importActual('vue')
  return {
    ...actual,
    defineComponent: vi.fn((options) => options),
    computed: vi.fn((getter) => ({
      get: getter,
      set: vi.fn()
    })),
    ref: vi.fn((value) => ({
      value,
      __v_isRef: true
    })),
    reactive: vi.fn((obj) => obj),
    watch: vi.fn(),
    watchEffect: vi.fn(),
    onMounted: vi.fn(),
    onUnmounted: vi.fn(),
    nextTick: vi.fn(() => Promise.resolve()),
    getCurrentInstance: vi.fn(() => ({
      proxy: {},
      appContext: {}
    })),
    defineEmits: vi.fn(() => vi.fn())
  }
})

// 模拟 Element Plus
vi.mock('element-plus', () => ({
  default: {
    install: vi.fn()
  },
  ElPagination: {
    name: 'ElPagination',
    template: '<div class="el-pagination"></div>'
  },
  ElForm: {
    name: 'ElForm',
    template: '<form class="el-form"></form>'
  },
  ElFormItem: {
    name: 'ElFormItem',
    template: '<div class="el-form-item"></div>'
  },
  ElInput: {
    name: 'ElInput',
    template: '<input class="el-input" />'
  },
  ElButton: {
    name: 'ElButton',
    template: '<button class="el-button"></button>'
  },
  ElCheckbox: {
    name: 'ElCheckbox',
    template: '<input type="checkbox" class="el-checkbox" />'
  }
}))

// 模拟 js-cookie
vi.mock('js-cookie', () => ({
  default: {
    get: vi.fn(),
    set: vi.fn(),
    remove: vi.fn()
  }
}))

// 模拟 axios
vi.mock('axios', () => ({
  default: {
    create: vi.fn(() => ({
      get: vi.fn(),
      post: vi.fn(),
      put: vi.fn(),
      delete: vi.fn(),
      interceptors: {
        request: { use: vi.fn() },
        response: { use: vi.fn() }
      }
    }))
  }
}))

// 模拟 pinia
vi.mock('pinia', () => ({
  createPinia: vi.fn(() => ({})),
  defineStore: vi.fn(),
  setActivePinia: vi.fn()
}))

// 模拟 vue-router
vi.mock('vue-router', () => ({
  createRouter: vi.fn(),
  createWebHistory: vi.fn(),
  useRouter: vi.fn(() => ({
    push: vi.fn(),
    replace: vi.fn(),
    go: vi.fn(),
    back: vi.fn()
  })),
  useRoute: vi.fn(() => ({
    params: {},
    query: {},
    path: '/'
  }))
}))

// 模拟全局属性
global.ResizeObserver = vi.fn().mockImplementation(() => ({
  observe: vi.fn(),
  unobserve: vi.fn(),
  disconnect: vi.fn()
}))

global.IntersectionObserver = vi.fn().mockImplementation(() => ({
  observe: vi.fn(),
  unobserve: vi.fn(),
  disconnect: vi.fn()
}))

// 模拟 window.matchMedia
Object.defineProperty(window, 'matchMedia', {
  writable: true,
  value: vi.fn().mockImplementation(query => ({
    matches: false,
    media: query,
    onchange: null,
    addListener: vi.fn(),
    removeListener: vi.fn(),
    addEventListener: vi.fn(),
    removeEventListener: vi.fn(),
    dispatchEvent: vi.fn()
  }))
})
